﻿/* ********************************************************************************
 * 名称：SysRoleAction_RepositoryService
 * 描述：系统角色关联  仓储服务接口实现类（本类通过CodeSmith软件生成生成）
 * 创建人：HFL
 * 创建日期：2020-06-01 17:48:55
 * 修改人：
 * 修改日期：
 * *******************************************************************************/
using WPay.Domain.MainModule.Interface.RepositoryServices;
using WPay.Domain.Model.DTOs;
using WPay.Domain.Model.Entities;
using WPay.Infrastructure.Repository.Base;
using WPay.Infrastructure.Repository.DapperCore;
using WPay.Infrastructure.Unity;
using Dapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace WPay.Infrastructure.Repository.MainModule
{
    /// <summary>
    /// 系统角色关联 仓储服务接口实现类
    /// </summary>
    public class SysRoleAction_RepositoryService : RepositoryBase, ISysRoleAction_RepositoryService
    {
        /// <summary>
        /// 查询SQL语句
        /// </summary>
        private readonly string querySql = @"select ID,RoleID,ModuleID,Weight from SysRoleAction where 1=1";
        /// <summary>
        /// 查询数据条数SQL语句
        /// </summary>
        private readonly string queryCountSql = @"select count(1) from SysRoleAction where 1=1";
        /// <summary>
        /// 新增SQL语句
        /// </summary>
        private readonly string addSql = @"insert into SysRoleAction(ID,RoleID,ModuleID,Weight) values (@ID,@RoleID,@ModuleID,@Weight)";
        /// <summary>
        /// 修改所有字段SQL语句
        /// </summary>
        private readonly string editSql = @"update SysRoleAction set RoleID=@RoleID,ModuleID=@ModuleID,Weight=@Weight where 1=1";
        /// <summary>
        /// 表主键where条件SQL语句
        /// </summary>
        private readonly string whereByPrimaryKey = @" and ID=@ID";

        #region 由软件生成，禁止修改，若需要扩展，请在下方进行扩展

        /// <summary>
        /// 根据主键获取系统角色关联资料信息
        /// </summary>
        /// <param name="_iD"></param>
        /// <returns>返回单个系统角色关联数据实体</returns>
        public SysRoleAction Get(string _iD)
        {
            string sql = querySql + whereByPrimaryKey;
            DynamicParameters param = new DynamicParameters();
            param.Add("ID", _iD);
            return base.QueryBySql<SysRoleAction>(sql, param);
        }

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="_cpnID">企业代码</param>
        /// <returns>返回系统角色关联数据集合</returns>
        public List<SysRoleAction> GetAll(string _cpnID)
        {
            return base.QueryListBySql<SysRoleAction>(querySql + " and CpnID=@CpnID", new { CpnID = _cpnID });
        }

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="_param">查询条件字典，其中key必须使用字段名</param>
        /// <returns>返回系统角色关联数据集合</returns>
        public List<SysRoleAction> GetAll(Hashtable _param)
        {
            //查询SQL语句组装
            string sql = querySql + base.GetQueryConditions(_param);
            //封装参数
            DynamicParameters dynamicParam = new DynamicParameters();
            foreach (string key in _param.Keys)
            {
                dynamicParam.Add(key, _param[key]);
            }

            return base.QueryListBySql<SysRoleAction>(sql, dynamicParam);
        }

        /// <summary>
        /// 按照分页条件查询数据集
        /// </summary>
        /// <param name="_param">查询条件字典，其中key必须使用字段名</param>
        /// <param name="_pageIndex">查询页码，从1开始，必传</param>
        /// <param name="_pageSize">每页查询数据条数，必传</param>
        /// <param name="_sort">排序列名，若要降序请用列名+" DESC"，必传</param>
        /// <param name="_pageCount">返回总页码数</param>
        /// <param name="_dataCount">返回总数据条数</param>
        /// <returns>返回系统角色关联数据集合</returns>
        public List<SysRoleAction> GetByPage(Hashtable _param, int _pageIndex, int _pageSize, string _sort, out int _pageCount, out int _dataCount)
        {
            if (_pageIndex == 0 || _pageSize == 0 || string.IsNullOrWhiteSpace(_sort))
            {
                _dataCount = 0;
                _pageCount = 0;
                return null;
            }
            //封装参数
            DynamicParameters dynamicParam = new DynamicParameters();
            foreach (string key in _param.Keys)
                dynamicParam.Add(key, _param[key]);
            //组装计数sql语句
            string countSql = queryCountSql + base.GetQueryConditions(_param);
            _dataCount = base.QueryScalarBySql<Int32>(countSql, dynamicParam);//获取总数据条数
            _pageCount = (_dataCount + _pageSize - 1) / _pageSize;//总页数 =  (数据总条数 + 每页显示条数 - 1) / 每页显示条数
            if (_dataCount == 0)
            {
                return new List<SysRoleAction>();
            }
            //组装分页查询语句
            string sql = querySql + base.GetQueryConditions(_param, _pageIndex, _pageSize, _sort);

            return base.QueryListBySql<SysRoleAction>(sql, dynamicParam);
        }

        /// <summary>
        /// 插入系统角色关联数据
        /// </summary>
        /// <param name="_sysRoleAction">需要插入的系统角色关联数据实体</param>
        /// <returns>返回影响数据行数</returns>
        public int Add(SysRoleAction _sysRoleAction)
        {
            return base.ExecuteBySql(addSql, _sysRoleAction);
        }

        /// <summary>
        /// 批量插入系统角色关联数据
        /// </summary>
        /// <param name="_sysRoleActionList">需要插入的系统角色关联数据集合</param>
        /// <returns>返回影响数据行数</returns>
        public int Add(List<SysRoleAction> _sysRoleActionList)
        {
            return base.BulkInsert(_sysRoleActionList);
        }

        /// <summary>
        /// 根据主键更新系统角色关联数据
        /// </summary>
        /// <param name="_sysRoleAction">需要更新的系统角色关联数据实体</param>
        /// <returns>返回影响数据行数</returns>
        public int Update(SysRoleAction _sysRoleAction)
        {
            return base.ExecuteBySql(editSql + whereByPrimaryKey, _sysRoleAction);
        }

        /// <summary>
        /// 根据主键更新系统角色关联指定字段
        /// </summary>
        /// <param name="_sysRoleAction">需要更新的系统角色关联数据实体</param>
        /// <param name="_updateProNames">需要更新的字段名</param>
        /// <returns>返回影响数据行数</returns>
        public int Update(SysRoleAction _sysRoleAction, params string[] _updateProNames)
        {
            string sql = @"update SysRoleAction set ";
            //主键值在修改时，禁止为空

            sql += base.GetUpdateSet(_updateProNames);
            return base.ExecuteBySql(sql + whereByPrimaryKey, _sysRoleAction);
        }

        #endregion

        /*业务扩展代码*/
        #region 业务扩展代码


        public List<SysRoleAction> GetSysRoleActions(IEnumerable<string> ids)
        {
            var dbids = new List<string>();
            ids.ToList().ForEach(x =>
            {
                dbids.Add($"'{x}'");
            });
            var roleids = string.Join(",", dbids);
            DynamicParameters param = new DynamicParameters();
            string sql = querySql + $" and RoleID in ({roleids})";
            return base.QueryListBySql<SysRoleAction>(sql);
        }
        #endregion
    }
}
